package icu.xblogs.report.utils;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelUtil {
    static Map<String,String> recordDict = new HashMap<String,String>(){{
        put("id","工号");
        put("name","姓名");
        put("attendanceRate","出勤率");
        put("attendanceDay","出勤天数");
        put("absenceDay","缺勤天数");
        put("time","出勤时间");
        put("lateDay","迟到天数");
    }};
    public static Workbook listToExcel(List<Map<String,String>> list){
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        if(!list.isEmpty()){
            int r = 0;
            int c = 0;
            Row titleRow = sheet.createRow(r++);
            for (String key : list.get(0).keySet()) {
                Cell cell = titleRow.createCell(c++);
                cell.setCellValue(recordDict.get(key));
            }
            for (Map<String, String> record : list) {
                Row row = sheet.createRow(r++);
                c = 0;
                for (String value : record.values()) {
                        Cell cell = row.createCell(c++);
                        cell.setCellValue(value);
                }
            }
        }
        return workbook;
    }
}
